package com.dl7.mvp.music.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.dl7.mvp.CommonConstant;
import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.base.BaseFragment;
import com.dl7.mvp.base.baseadapter.BaseRecyclerViewAdapter;
import com.dl7.mvp.music.adapter.MusicAlbumListAdapter;
import com.dl7.mvp.music.bean.MusicAlbumList;
import com.dl7.mvp.mvp.R;
import com.dl7.mvp.mvp.databinding.BaseRecyclerviewVerticalBinding;
import com.dl7.mvp.video.bean.VideoCategoryList;

import java.util.ArrayList;
import java.util.List;


public class MusicAlbumListFragment extends BaseFragment<BaseRecyclerviewVerticalBinding> {
    //getRcmdMusicCategoryList
    public MusicAlbumListFragment() {
        // Required empty public constructor
    }

    @Override
    public int getContentViewResID() {
        return R.layout.base_recyclerview_vertical;
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyclerView();
        if (getArguments() != null) {
            int id = getArguments().getInt(CommonConstant.CATEGORY_ID);
            RetrofitService.getMusicAlbumList(id, 0, 100)
                    .subscribe(albumList -> {
                        setData(albumList);
                    });
        }
    }

    private void initRecyclerView() {
        bindingView.recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    }

    public static MusicAlbumListFragment newInstance(int categoryID) {
        MusicAlbumListFragment fragment = new MusicAlbumListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(CommonConstant.CATEGORY_ID, categoryID);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void setData(List<MusicAlbumList.DataBean> albumList) {
        MusicAlbumListAdapter adapter = new MusicAlbumListAdapter();
        adapter.addDatas((ArrayList<MusicAlbumList.DataBean>) albumList);
        bindingView.recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnItemClickListener(new MyOnItemClickListener());
    }

    class MyOnItemClickListener implements BaseRecyclerViewAdapter.OnItemClickListener<MusicAlbumList.DataBean> {
        @Override
        public void onItemClick(int position, MusicAlbumList.DataBean data) {
            // TODO
            MusicAlbumDetailActivity.launch(getContext(), data.getId(), data.getName());
        }
    }
}
